#==============================================================================
# Theta Scripting Engine - Unarmed by privateer
#==============================================================================
# ► Version  : 1.0
#==============================================================================
# ► Introduction
# This script will enable you to customize how an unarmed actor attacks. You
# can set a custom damage formula and custom attack animations. Useful if you
# want to create actors or classes that use different damage formula when they
# are unarmed.
#==============================================================================
# ► Instruction
# Just put this script anywhere below Scene_Debug but above Main.
#==============================================================================
# ► Compatibility
# Designed and tested for DBS. May or not work with exotic CBS, Custom Equipment 
# System (Multi-Equip and so on) and/or scripts that alter how animation id is 
# defined or how an attack is done.
#==============================================================================

module TSE
  module Unarmed
    # State ID(s) that will make its bearer deal custom damage formula when
    # he/she is unarmed.
    UNARMED_STATES = [23]
    
    # Class ID(s) that will use custom damage formula when they are unarmed by
    # default.
    UNARMED_CLASSES = []
    
    # Actor ID(s) that will use custom damage formula when they are unarmed by
    # default.
    UNARMED_ACTORS = []
    
    # Animation ID used on the attacker if the attacker is unarmed.
    ATTACKER_ANIMATION_ID = 0
    
    # Animation ID used on the target if the attacker is unarmed.
    TARGET_ANIMATION_ID = 4
    
    # Custom Damage Formula
    # Put everything in the "", otherwise it will generate an error.
    DAMAGE_FORMULA = "[attacker.str - self.pdef / 2, 0].max * (20 + attacker.str) / 20"
  end
end

#==============================================================================
# DON'T EDIT ANY FURTHER UNLESS YOU KNOW WHAT YOU'RE DOING!
#==============================================================================

$imported = {} if $imported.nil?
$imported["TSE-Unarmed"] = true

module RPG
  class State
    def unarmed_state?
      TSE::Unarmed::UNARMED_STATES.include?(id)
    end
  end
  class Actor
    def unarmed_actor?
      TSE::Unarmed::UNARMED_ACTORS.include?(id)
    end
  end  
  class Class
    def unarmed_class?
      TSE::Unarmed::UNARMED_CLASSES.include?(id)
    end
  end    
end

class Game_Battler
  include TSE
  include Unarmed  
  alias tse_unarmed_attack_effect attack_effect
  def attack_effect(attacker)
    return tse_unarmed_attack_effect(attacker) if attacker.is_a?(Game_Enemy)
    if attacker.is_unarmed? and attacker.deal_unarmed_damage?
      self.critical = false
      hit_result = (rand(100) < attacker.hit)
      if hit_result
        self.damage = eval DAMAGE_FORMULA
        self.damage *= elements_correct(attacker.element_set)
        self.damage /= 100
        if self.damage > 0
          if rand(100) < 4 * attacker.dex / self.agi
            self.damage *= 2
            self.critical = true
          end
          if self.guarding?
            self.damage /= 2
          end
        end
        if self.damage.abs > 0
          amp = [self.damage.abs * 15 / 100, 1].max
          self.damage += rand(amp+1) + rand(amp+1) - amp
        end
        eva = 8 * self.agi / attacker.dex + self.eva
        hit = self.damage < 0 ? 100 : 100 - eva
        hit = self.cant_evade? ? 100 : hit
        hit_result = (rand(100) < hit)
      end
      if hit_result
        remove_states_shock
        self.hp -= self.damage
        @state_changed = false
        states_plus(attacker.plus_state_set)
        states_minus(attacker.minus_state_set)
      else
        self.damage = "Miss"
        self.critical = false
      end
      return true
    else
      tse_unarmed_attack_effect(attacker)
    end
  end  
end
class Game_Actor < Game_Battler
  attr_reader :actor_id
  include TSE
  include Unarmed    
  def is_unarmed?
    self.weapon_id == 0
  end
  alias tse_unarmed_anim1_id animation1_id
  def animation1_id
    if self.is_unarmed?
      return ATTACKER_ANIMATION_ID
    else
      return tse_unarmed_anim1_id
    end
  end
  alias tse_unarmed_anim2_id animation2_id
  def animation2_id
    if self.is_unarmed?
      return TARGET_ANIMATION_ID
    else
      return tse_unarmed_anim2_id
    end
  end
  def deal_unarmed_damage?
    return true if @states.any?{|s|$data_states[s].unarmed_state?}
    return true if $data_classes[self.class_id].unarmed_class?
    return true if $data_actors[self.actor_id].unarmed_actor?
  end
end
